{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6a821648",
   "metadata": {},
   "source": [
    "# 使用TensorFlow Lite Model Maker生成图像分类器模型\n",
    "\n",
    "本教程使用[TensorFlow Lite Model Maker library](https://www.tensorflow.org/lite/guide/model_maker)来生成[图像分类](https://so.csdn.net/so/search?q=图像分类&spm=1001.2101.3001.7020)器模型。TensorFlow Lite Model Maker可以简化使用自定义数据集训练 TensorFlow Lite 模型的过程。该库使用迁移学习来减少所需的训练数据量并缩短训练时间。\n",
    "\n",
    "# 预备工作\n",
    "\n",
    "首先安装程序运行必备的一些库。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c4b0cf86",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking in indexes: http://pypi.douban.com/simple/\n",
      "Requirement already satisfied: pip in e:\\softwares\\anaconda\\lib\\site-packages (21.2.4)\n",
      "Requirement already satisfied: install in e:\\softwares\\anaconda\\lib\\site-packages (1.3.5)\n",
      "Requirement already satisfied: tflite-model-maker in e:\\softwares\\anaconda\\lib\\site-packages (0.3.4)\n",
      "Requirement already satisfied: numpy>=1.17.3 in e:\\softwares\\anaconda\\lib\\site-packages (from tflite-model-maker) (1.20.3)\n",
      "Requirement already satisfied: tensorflow-hub<0.13,>=0.7.0 in e:\\softwares\\anaconda\\lib\\site-packages (from tflite-model-maker) (0.12.0)\n",
      "Requirement already satisfied: tensorflow-model-optimization>=0.5 in e:\\softwares\\anaconda\\lib\\site-packages (from tflite-model-maker) (0.7.2)\n",
      "Requirement already satisfied: librosa==0.8.1 in e:\\softwares\\anaconda\\lib\\site-packages (from tflite-model-maker) (0.8.1)\n",
      "Requirement already satisfied: tensorflow-datasets>=2.1.0 in e:\\softwares\\anaconda\\lib\\site-packages (from tflite-model-maker) (4.5.2)\n",
      "Requirement already satisfied: sentencepiece>=0.1.91 in e:\\softwares\\anaconda\\lib\\site-packages (from tflite-model-maker) (0.1.96)\n",
      "Requirement already satisfied: numba==0.53 in e:\\softwares\\anaconda\\lib\\site-packages (from tflite-model-maker) (0.53.0)\n",
      "Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in e:\\softwares\\anaconda\\lib\\site-packages (from tflite-model-maker) (1.25.11)\n",
      "Requirement already satisfied: tf-models-official==2.3.0 in e:\\softwares\\anaconda\\lib\\site-packages (from tflite-model-maker) (2.3.0)\n",
      "Requirement already satisfied: PyYAML>=5.1 in e:\\softwares\\anaconda\\lib\\site-packages (from tflite-model-maker) (6.0)\n",
      "Requirement already satisfied: tflite-support>=0.3.1 in e:\\softwares\\anaconda\\lib\\site-packages (from tflite-model-maker) (0.4.0)\n",
      "Requirement already satisfied: tensorflowjs>=2.4.0 in e:\\softwares\\anaconda\\lib\\site-packages (from tflite-model-maker) (3.18.0)\n",
      "Requirement already satisfied: fire>=0.3.1 in e:\\softwares\\anaconda\\lib\\site-packages (from tflite-model-maker) (0.4.0)\n",
      "Requirement already satisfied: Cython>=0.29.13 in e:\\softwares\\anaconda\\lib\\site-packages (from tflite-model-maker) (0.29.24)\n",
      "Requirement already satisfied: six>=1.12.0 in e:\\softwares\\anaconda\\lib\\site-packages (from tflite-model-maker) (1.16.0)\n",
      "Requirement already satisfied: tensorflow-addons>=0.11.2 in e:\\softwares\\anaconda\\lib\\site-packages (from tflite-model-maker) (0.17.0)\n",
      "Requirement already satisfied: neural-structured-learning>=1.3.1 in e:\\softwares\\anaconda\\lib\\site-packages (from tflite-model-maker) (1.3.1)\n",
      "Requirement already satisfied: lxml>=4.6.1 in e:\\softwares\\anaconda\\lib\\site-packages (from tflite-model-maker) (4.6.3)\n",
      "Requirement already satisfied: tensorflow>=2.6.0 in e:\\softwares\\anaconda\\lib\\site-packages (from tflite-model-maker) (2.9.1)\n",
      "Requirement already satisfied: pillow>=7.0.0 in e:\\softwares\\anaconda\\lib\\site-packages (from tflite-model-maker) (9.0.1)\n",
      "Requirement already satisfied: flatbuffers==1.12 in e:\\softwares\\anaconda\\lib\\site-packages (from tflite-model-maker) (1.12)\n",
      "Requirement already satisfied: absl-py>=0.10.0 in e:\\softwares\\anaconda\\lib\\site-packages (from tflite-model-maker) (1.0.0)\n",
      "Requirement already satisfied: matplotlib<3.5.0,>=3.0.3 in e:\\softwares\\anaconda\\lib\\site-packages (from tflite-model-maker) (3.4.3)\n",
      "Requirement already satisfied: audioread>=2.0.0 in e:\\softwares\\anaconda\\lib\\site-packages (from librosa==0.8.1->tflite-model-maker) (2.1.9)\n",
      "Requirement already satisfied: decorator>=3.0.0 in e:\\softwares\\anaconda\\lib\\site-packages (from librosa==0.8.1->tflite-model-maker) (5.1.1)\n",
      "Requirement already satisfied: pooch>=1.0 in e:\\softwares\\anaconda\\lib\\site-packages (from librosa==0.8.1->tflite-model-maker) (1.6.0)\n",
      "Requirement already satisfied: scipy>=1.0.0 in e:\\softwares\\anaconda\\lib\\site-packages (from librosa==0.8.1->tflite-model-maker) (1.7.1)\n",
      "Requirement already satisfied: packaging>=20.0 in e:\\softwares\\anaconda\\lib\\site-packages (from librosa==0.8.1->tflite-model-maker) (20.9)\n",
      "Requirement already satisfied: scikit-learn!=0.19.0,>=0.14.0 in e:\\softwares\\anaconda\\lib\\site-packages (from librosa==0.8.1->tflite-model-maker) (0.24.2)\n",
      "Requirement already satisfied: resampy>=0.2.2 in e:\\softwares\\anaconda\\lib\\site-packages (from librosa==0.8.1->tflite-model-maker) (0.2.2)\n",
      "Requirement already satisfied: joblib>=0.14 in e:\\softwares\\anaconda\\lib\\site-packages (from librosa==0.8.1->tflite-model-maker) (1.1.0)\n",
      "Requirement already satisfied: soundfile>=0.10.2 in e:\\softwares\\anaconda\\lib\\site-packages (from librosa==0.8.1->tflite-model-maker) (0.10.3.post1)\n",
      "Requirement already satisfied: setuptools in e:\\softwares\\anaconda\\lib\\site-packages (from numba==0.53->tflite-model-maker) (61.2.0)\n",
      "Requirement already satisfied: llvmlite<0.37,>=0.36.0rc1 in e:\\softwares\\anaconda\\lib\\site-packages (from numba==0.53->tflite-model-maker) (0.36.0)\n",
      "Requirement already satisfied: kaggle>=1.3.9 in e:\\softwares\\anaconda\\lib\\site-packages (from tf-models-official==2.3.0->tflite-model-maker) (1.5.12)\n",
      "Requirement already satisfied: psutil>=5.4.3 in e:\\softwares\\anaconda\\lib\\site-packages (from tf-models-official==2.3.0->tflite-model-maker) (5.8.0)\n",
      "Requirement already satisfied: tf-slim>=1.1.0 in e:\\softwares\\anaconda\\lib\\site-packages (from tf-models-official==2.3.0->tflite-model-maker) (1.1.0)\n",
      "Requirement already satisfied: pandas>=0.22.0 in e:\\softwares\\anaconda\\lib\\site-packages (from tf-models-official==2.3.0->tflite-model-maker) (1.3.4)\n",
      "Requirement already satisfied: dataclasses in e:\\softwares\\anaconda\\lib\\site-packages (from tf-models-official==2.3.0->tflite-model-maker) (0.6)\n",
      "Requirement already satisfied: google-api-python-client>=1.6.7 in e:\\softwares\\anaconda\\lib\\site-packages (from tf-models-official==2.3.0->tflite-model-maker) (2.49.0)\n",
      "Requirement already satisfied: py-cpuinfo>=3.3.0 in e:\\softwares\\anaconda\\lib\\site-packages (from tf-models-official==2.3.0->tflite-model-maker) (8.0.0)\n",
      "Requirement already satisfied: gin-config in e:\\softwares\\anaconda\\lib\\site-packages (from tf-models-official==2.3.0->tflite-model-maker) (0.5.0)\n",
      "Requirement already satisfied: opencv-python-headless in e:\\softwares\\anaconda\\lib\\site-packages (from tf-models-official==2.3.0->tflite-model-maker) (4.5.5.64)\n",
      "Requirement already satisfied: google-cloud-bigquery>=0.31.0 in e:\\softwares\\anaconda\\lib\\site-packages (from tf-models-official==2.3.0->tflite-model-maker) (3.1.0)\n",
      "Requirement already satisfied: termcolor in e:\\softwares\\anaconda\\lib\\site-packages (from fire>=0.3.1->tflite-model-maker) (1.1.0)\n",
      "Requirement already satisfied: google-auth<3.0.0dev,>=1.16.0 in e:\\softwares\\anaconda\\lib\\site-packages (from google-api-python-client>=1.6.7->tf-models-official==2.3.0->tflite-model-maker) (2.6.6)\n",
      "Requirement already satisfied: google-api-core!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.0,<3.0.0dev,>=1.31.5 in e:\\softwares\\anaconda\\lib\\site-packages (from google-api-python-client>=1.6.7->tf-models-official==2.3.0->tflite-model-maker) (2.8.1)\n",
      "Requirement already satisfied: google-auth-httplib2>=0.1.0 in e:\\softwares\\anaconda\\lib\\site-packages (from google-api-python-client>=1.6.7->tf-models-official==2.3.0->tflite-model-maker) (0.1.0)\n",
      "Requirement already satisfied: uritemplate<5,>=3.0.1 in e:\\softwares\\anaconda\\lib\\site-packages (from google-api-python-client>=1.6.7->tf-models-official==2.3.0->tflite-model-maker) (4.1.1)\n",
      "Requirement already satisfied: httplib2<1dev,>=0.15.0 in e:\\softwares\\anaconda\\lib\\site-packages (from google-api-python-client>=1.6.7->tf-models-official==2.3.0->tflite-model-maker) (0.20.4)\n",
      "Requirement already satisfied: requests<3.0.0dev,>=2.18.0 in e:\\softwares\\anaconda\\lib\\site-packages (from google-api-core!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.0,<3.0.0dev,>=1.31.5->google-api-python-client>=1.6.7->tf-models-official==2.3.0->tflite-model-maker) (2.26.0)\n",
      "Requirement already satisfied: googleapis-common-protos<2.0dev,>=1.56.2 in e:\\softwares\\anaconda\\lib\\site-packages (from google-api-core!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.0,<3.0.0dev,>=1.31.5->google-api-python-client>=1.6.7->tf-models-official==2.3.0->tflite-model-maker) (1.56.2)\n",
      "Requirement already satisfied: protobuf<4.0.0dev,>=3.15.0 in e:\\softwares\\anaconda\\lib\\site-packages (from google-api-core!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.0,<3.0.0dev,>=1.31.5->google-api-python-client>=1.6.7->tf-models-official==2.3.0->tflite-model-maker) (3.19.4)\n",
      "Requirement already satisfied: cachetools<6.0,>=2.0.0 in e:\\softwares\\anaconda\\lib\\site-packages (from google-auth<3.0.0dev,>=1.16.0->google-api-python-client>=1.6.7->tf-models-official==2.3.0->tflite-model-maker) (5.1.0)\n",
      "Requirement already satisfied: pyasn1-modules>=0.2.1 in e:\\softwares\\anaconda\\lib\\site-packages (from google-auth<3.0.0dev,>=1.16.0->google-api-python-client>=1.6.7->tf-models-official==2.3.0->tflite-model-maker) (0.2.8)\n",
      "Requirement already satisfied: rsa<5,>=3.1.4 in e:\\softwares\\anaconda\\lib\\site-packages (from google-auth<3.0.0dev,>=1.16.0->google-api-python-client>=1.6.7->tf-models-official==2.3.0->tflite-model-maker) (4.8)\n",
      "Requirement already satisfied: python-dateutil<3.0dev,>=2.7.2 in e:\\softwares\\anaconda\\lib\\site-packages (from google-cloud-bigquery>=0.31.0->tf-models-official==2.3.0->tflite-model-maker) (2.8.2)\n",
      "Requirement already satisfied: google-cloud-bigquery-storage<3.0.0dev,>=2.0.0 in e:\\softwares\\anaconda\\lib\\site-packages (from google-cloud-bigquery>=0.31.0->tf-models-official==2.3.0->tflite-model-maker) (2.13.1)\n",
      "Requirement already satisfied: google-cloud-core<3.0.0dev,>=1.4.1 in e:\\softwares\\anaconda\\lib\\site-packages (from google-cloud-bigquery>=0.31.0->tf-models-official==2.3.0->tflite-model-maker) (2.3.0)\n",
      "Requirement already satisfied: grpcio<2.0dev,>=1.38.1 in e:\\softwares\\anaconda\\lib\\site-packages (from google-cloud-bigquery>=0.31.0->tf-models-official==2.3.0->tflite-model-maker) (1.46.3)\n",
      "Requirement already satisfied: google-resumable-media<3.0dev,>=0.6.0 in e:\\softwares\\anaconda\\lib\\site-packages (from google-cloud-bigquery>=0.31.0->tf-models-official==2.3.0->tflite-model-maker) (2.3.3)\n",
      "Requirement already satisfied: proto-plus>=1.15.0 in e:\\softwares\\anaconda\\lib\\site-packages (from google-cloud-bigquery>=0.31.0->tf-models-official==2.3.0->tflite-model-maker) (1.20.5)\n",
      "Requirement already satisfied: pyarrow<9.0dev,>=3.0.0 in e:\\softwares\\anaconda\\lib\\site-packages (from google-cloud-bigquery>=0.31.0->tf-models-official==2.3.0->tflite-model-maker) (8.0.0)\n",
      "Requirement already satisfied: grpcio-status<2.0dev,>=1.33.2 in e:\\softwares\\anaconda\\lib\\site-packages (from google-api-core!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.0,<3.0.0dev,>=1.31.5->google-api-python-client>=1.6.7->tf-models-official==2.3.0->tflite-model-maker) (1.46.3)\n",
      "Requirement already satisfied: google-crc32c<2.0dev,>=1.0 in e:\\softwares\\anaconda\\lib\\site-packages (from google-resumable-media<3.0dev,>=0.6.0->google-cloud-bigquery>=0.31.0->tf-models-official==2.3.0->tflite-model-maker) (1.3.0)\n",
      "Requirement already satisfied: pyparsing!=3.0.0,!=3.0.1,!=3.0.2,!=3.0.3,<4,>=2.4.2 in e:\\softwares\\anaconda\\lib\\site-packages (from httplib2<1dev,>=0.15.0->google-api-python-client>=1.6.7->tf-models-official==2.3.0->tflite-model-maker) (3.0.4)\n",
      "Requirement already satisfied: certifi in e:\\softwares\\anaconda\\lib\\site-packages (from kaggle>=1.3.9->tf-models-official==2.3.0->tflite-model-maker) (2022.5.18.1)\n",
      "Requirement already satisfied: python-slugify in e:\\softwares\\anaconda\\lib\\site-packages (from kaggle>=1.3.9->tf-models-official==2.3.0->tflite-model-maker) (5.0.2)\n",
      "Requirement already satisfied: tqdm in e:\\softwares\\anaconda\\lib\\site-packages (from kaggle>=1.3.9->tf-models-official==2.3.0->tflite-model-maker) (4.64.0)\n",
      "Requirement already satisfied: cycler>=0.10 in e:\\softwares\\anaconda\\lib\\site-packages (from matplotlib<3.5.0,>=3.0.3->tflite-model-maker) (0.10.0)\n",
      "Requirement already satisfied: kiwisolver>=1.0.1 in e:\\softwares\\anaconda\\lib\\site-packages (from matplotlib<3.5.0,>=3.0.3->tflite-model-maker) (1.3.1)\n",
      "Requirement already satisfied: attrs in e:\\softwares\\anaconda\\lib\\site-packages (from neural-structured-learning>=1.3.1->tflite-model-maker) (21.4.0)\n",
      "Requirement already satisfied: pytz>=2017.3 in e:\\softwares\\anaconda\\lib\\site-packages (from pandas>=0.22.0->tf-models-official==2.3.0->tflite-model-maker) (2021.3)\n",
      "Requirement already satisfied: appdirs>=1.3.0 in e:\\softwares\\anaconda\\lib\\site-packages (from pooch>=1.0->librosa==0.8.1->tflite-model-maker) (1.4.4)\n",
      "Requirement already satisfied: pyasn1<0.5.0,>=0.4.6 in e:\\softwares\\anaconda\\lib\\site-packages (from pyasn1-modules>=0.2.1->google-auth<3.0.0dev,>=1.16.0->google-api-python-client>=1.6.7->tf-models-official==2.3.0->tflite-model-maker) (0.4.8)\n",
      "Requirement already satisfied: idna<4,>=2.5 in e:\\softwares\\anaconda\\lib\\site-packages (from requests<3.0.0dev,>=2.18.0->google-api-core!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.0,<3.0.0dev,>=1.31.5->google-api-python-client>=1.6.7->tf-models-official==2.3.0->tflite-model-maker) (3.3)\n",
      "Requirement already satisfied: charset-normalizer~=2.0.0 in e:\\softwares\\anaconda\\lib\\site-packages (from requests<3.0.0dev,>=2.18.0->google-api-core!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.0,<3.0.0dev,>=1.31.5->google-api-python-client>=1.6.7->tf-models-official==2.3.0->tflite-model-maker) (2.0.4)\n",
      "Requirement already satisfied: threadpoolctl>=2.0.0 in e:\\softwares\\anaconda\\lib\\site-packages (from scikit-learn!=0.19.0,>=0.14.0->librosa==0.8.1->tflite-model-maker) (2.2.0)\n",
      "Requirement already satisfied: cffi>=1.0 in e:\\softwares\\anaconda\\lib\\site-packages (from soundfile>=0.10.2->librosa==0.8.1->tflite-model-maker) (1.15.0)\n",
      "Requirement already satisfied: pycparser in e:\\softwares\\anaconda\\lib\\site-packages (from cffi>=1.0->soundfile>=0.10.2->librosa==0.8.1->tflite-model-maker) (2.21)\n",
      "Requirement already satisfied: libclang>=13.0.0 in e:\\softwares\\anaconda\\lib\\site-packages (from tensorflow>=2.6.0->tflite-model-maker) (14.0.1)\n",
      "Requirement already satisfied: keras-preprocessing>=1.1.1 in e:\\softwares\\anaconda\\lib\\site-packages (from tensorflow>=2.6.0->tflite-model-maker) (1.1.2)\n",
      "Requirement already satisfied: opt-einsum>=2.3.2 in e:\\softwares\\anaconda\\lib\\site-packages (from tensorflow>=2.6.0->tflite-model-maker) (3.3.0)\n",
      "Requirement already satisfied: keras<2.10.0,>=2.9.0rc0 in e:\\softwares\\anaconda\\lib\\site-packages (from tensorflow>=2.6.0->tflite-model-maker) (2.9.0)\n",
      "Requirement already satisfied: tensorflow-io-gcs-filesystem>=0.23.1 in e:\\softwares\\anaconda\\lib\\site-packages (from tensorflow>=2.6.0->tflite-model-maker) (0.26.0)\n",
      "Requirement already satisfied: h5py>=2.9.0 in e:\\softwares\\anaconda\\lib\\site-packages (from tensorflow>=2.6.0->tflite-model-maker) (3.2.1)\n",
      "Requirement already satisfied: wrapt>=1.11.0 in e:\\softwares\\anaconda\\lib\\site-packages (from tensorflow>=2.6.0->tflite-model-maker) (1.12.1)\n",
      "Requirement already satisfied: tensorflow-estimator<2.10.0,>=2.9.0rc0 in e:\\softwares\\anaconda\\lib\\site-packages (from tensorflow>=2.6.0->tflite-model-maker) (2.9.0)\n",
      "Requirement already satisfied: google-pasta>=0.1.1 in e:\\softwares\\anaconda\\lib\\site-packages (from tensorflow>=2.6.0->tflite-model-maker) (0.2.0)\n",
      "Requirement already satisfied: gast<=0.4.0,>=0.2.1 in e:\\softwares\\anaconda\\lib\\site-packages (from tensorflow>=2.6.0->tflite-model-maker) (0.4.0)\n",
      "Requirement already satisfied: astunparse>=1.6.0 in e:\\softwares\\anaconda\\lib\\site-packages (from tensorflow>=2.6.0->tflite-model-maker) (1.6.3)\n",
      "Requirement already satisfied: typing-extensions>=3.6.6 in e:\\softwares\\anaconda\\lib\\site-packages (from tensorflow>=2.6.0->tflite-model-maker) (4.1.1)\n",
      "Requirement already satisfied: tensorboard<2.10,>=2.9 in e:\\softwares\\anaconda\\lib\\site-packages (from tensorflow>=2.6.0->tflite-model-maker) (2.9.0)\n",
      "Requirement already satisfied: wheel<1.0,>=0.23.0 in e:\\softwares\\anaconda\\lib\\site-packages (from astunparse>=1.6.0->tensorflow>=2.6.0->tflite-model-maker) (0.37.1)\n",
      "Requirement already satisfied: werkzeug>=1.0.1 in e:\\softwares\\anaconda\\lib\\site-packages (from tensorboard<2.10,>=2.9->tensorflow>=2.6.0->tflite-model-maker) (2.0.2)\n",
      "Requirement already satisfied: tensorboard-plugin-wit>=1.6.0 in e:\\softwares\\anaconda\\lib\\site-packages (from tensorboard<2.10,>=2.9->tensorflow>=2.6.0->tflite-model-maker) (1.8.1)\n",
      "Requirement already satisfied: markdown>=2.6.8 in e:\\softwares\\anaconda\\lib\\site-packages (from tensorboard<2.10,>=2.9->tensorflow>=2.6.0->tflite-model-maker) (3.3.7)\n",
      "Requirement already satisfied: tensorboard-data-server<0.7.0,>=0.6.0 in e:\\softwares\\anaconda\\lib\\site-packages (from tensorboard<2.10,>=2.9->tensorflow>=2.6.0->tflite-model-maker) (0.6.1)\n",
      "Requirement already satisfied: google-auth-oauthlib<0.5,>=0.4.1 in e:\\softwares\\anaconda\\lib\\site-packages (from tensorboard<2.10,>=2.9->tensorflow>=2.6.0->tflite-model-maker) (0.4.6)\n",
      "Requirement already satisfied: requests-oauthlib>=0.7.0 in e:\\softwares\\anaconda\\lib\\site-packages (from google-auth-oauthlib<0.5,>=0.4.1->tensorboard<2.10,>=2.9->tensorflow>=2.6.0->tflite-model-maker) (1.3.1)\n",
      "Requirement already satisfied: importlib-metadata>=4.4 in e:\\softwares\\anaconda\\lib\\site-packages (from markdown>=2.6.8->tensorboard<2.10,>=2.9->tensorflow>=2.6.0->tflite-model-maker) (4.11.3)\n",
      "Requirement already satisfied: zipp>=0.5 in e:\\softwares\\anaconda\\lib\\site-packages (from importlib-metadata>=4.4->markdown>=2.6.8->tensorboard<2.10,>=2.9->tensorflow>=2.6.0->tflite-model-maker) (3.8.0)\n",
      "Requirement already satisfied: oauthlib>=3.0.0 in e:\\softwares\\anaconda\\lib\\site-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<0.5,>=0.4.1->tensorboard<2.10,>=2.9->tensorflow>=2.6.0->tflite-model-maker) (3.2.0)\n",
      "Requirement already satisfied: typeguard>=2.7 in e:\\softwares\\anaconda\\lib\\site-packages (from tensorflow-addons>=0.11.2->tflite-model-maker) (2.13.3)\n",
      "Requirement already satisfied: dill in e:\\softwares\\anaconda\\lib\\site-packages (from tensorflow-datasets>=2.1.0->tflite-model-maker) (0.3.5.1)\n",
      "Requirement already satisfied: promise in e:\\softwares\\anaconda\\lib\\site-packages (from tensorflow-datasets>=2.1.0->tflite-model-maker) (2.3)\n",
      "Requirement already satisfied: tensorflow-metadata in e:\\softwares\\anaconda\\lib\\site-packages (from tensorflow-datasets>=2.1.0->tflite-model-maker) (1.8.0)\n",
      "Requirement already satisfied: dm-tree~=0.1.1 in e:\\softwares\\anaconda\\lib\\site-packages (from tensorflow-model-optimization>=0.5->tflite-model-maker) (0.1.7)\n",
      "Requirement already satisfied: pybind11>=2.6.0 in e:\\softwares\\anaconda\\lib\\site-packages (from tflite-support>=0.3.1->tflite-model-maker) (2.9.2)\n",
      "Requirement already satisfied: sounddevice>=0.4.4 in e:\\softwares\\anaconda\\lib\\site-packages (from tflite-support>=0.3.1->tflite-model-maker) (0.4.4)\n",
      "Requirement already satisfied: text-unidecode>=1.3 in e:\\softwares\\anaconda\\lib\\site-packages (from python-slugify->kaggle>=1.3.9->tf-models-official==2.3.0->tflite-model-maker) (1.3)\n",
      "Requirement already satisfied: colorama in e:\\softwares\\anaconda\\lib\\site-packages (from tqdm->kaggle>=1.3.9->tf-models-official==2.3.0->tflite-model-maker) (0.4.4)\n"
     ]
    }
   ],
   "source": [
    "!pip --default-timeout=1000 install pip install tflite-model-maker -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d39b9c8",
   "metadata": {},
   "source": [
    "安装的时候遇到ERROR: Cannot uninstall 'llvmlite'.的问题。首先卸载llvmlite包，这里利用Anaconda Navigator中Environments组件管理和卸载相关的Package。\n",
    "![image-20220527155046511](https://gitee.com/deer-guide/my-drawing-bed/raw/master/202205271550654.png)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c2a22acc",
   "metadata": {},
   "source": [
    "这里pip之前添加\"!\"符号是告诉notebook把pip安装指令当做shell指令执行（实际上其实不加！也能执行）。安装的时候遇到`ERROR: Cannot uninstall 'llvmlite'.`的问题。首先卸载llvmlite包，这里利用Anaconda Navigator中Environments组件管理和卸载相关的Package。\n",
    "解决之后，再次提示`conda-repo-cli 1.0.4`和`anaconda-project 0.10.1`没有安装。\n",
    "分别使用`pip install conda-repo-cli==1.0.4`和`pip install anaconda-project==0.10.1`安装相应的库。注意，由于Anaconda版本不同，安装tflite [model](https://so.csdn.net/so/search?q=model&spm=1001.2101.3001.7020) maker的环境不同，需根据实际情况自行解决安装过程中遇到的问题。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1d8ee2cf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: conda-repo-cli==1.0.4 in e:\\softwares\\anaconda\\lib\\site-packages (1.0.4)\n",
      "Requirement already satisfied: six in e:\\softwares\\anaconda\\lib\\site-packages (from conda-repo-cli==1.0.4) (1.16.0)\n",
      "Requirement already satisfied: python-dateutil>=2.6.1 in e:\\softwares\\anaconda\\lib\\site-packages (from conda-repo-cli==1.0.4) (2.8.2)\n",
      "Requirement already satisfied: nbformat>=4.4.0 in e:\\softwares\\anaconda\\lib\\site-packages (from conda-repo-cli==1.0.4) (5.3.0)\n",
      "Requirement already satisfied: pytz in e:\\softwares\\anaconda\\lib\\site-packages (from conda-repo-cli==1.0.4) (2021.3)\n",
      "Requirement already satisfied: PyYAML>=3.12 in e:\\softwares\\anaconda\\lib\\site-packages (from conda-repo-cli==1.0.4) (6.0)\n",
      "Requirement already satisfied: setuptools in e:\\softwares\\anaconda\\lib\\site-packages (from conda-repo-cli==1.0.4) (61.2.0)\n",
      "Requirement already satisfied: clyent>=1.2.0 in e:\\softwares\\anaconda\\lib\\site-packages (from conda-repo-cli==1.0.4) (1.2.2)\n",
      "Requirement already satisfied: pathlib in e:\\softwares\\anaconda\\lib\\site-packages (from conda-repo-cli==1.0.4) (1.0.1)\n",
      "Requirement already satisfied: requests>=2.9.1 in e:\\softwares\\anaconda\\lib\\site-packages (from conda-repo-cli==1.0.4) (2.26.0)\n",
      "Requirement already satisfied: traitlets>=4.1 in e:\\softwares\\anaconda\\lib\\site-packages (from nbformat>=4.4.0->conda-repo-cli==1.0.4) (5.1.1)\n",
      "Requirement already satisfied: fastjsonschema in e:\\softwares\\anaconda\\lib\\site-packages (from nbformat>=4.4.0->conda-repo-cli==1.0.4) (2.15.1)\n",
      "Requirement already satisfied: jupyter-core in e:\\softwares\\anaconda\\lib\\site-packages (from nbformat>=4.4.0->conda-repo-cli==1.0.4) (4.10.0)\n",
      "Requirement already satisfied: jsonschema>=2.6 in e:\\softwares\\anaconda\\lib\\site-packages (from nbformat>=4.4.0->conda-repo-cli==1.0.4) (4.4.0)\n",
      "Requirement already satisfied: pyrsistent!=0.17.0,!=0.17.1,!=0.17.2,>=0.14.0 in e:\\softwares\\anaconda\\lib\\site-packages (from jsonschema>=2.6->nbformat>=4.4.0->conda-repo-cli==1.0.4) (0.18.0)\n",
      "Requirement already satisfied: attrs>=17.4.0 in e:\\softwares\\anaconda\\lib\\site-packages (from jsonschema>=2.6->nbformat>=4.4.0->conda-repo-cli==1.0.4) (21.4.0)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in e:\\softwares\\anaconda\\lib\\site-packages (from requests>=2.9.1->conda-repo-cli==1.0.4) (2022.5.18.1)\n",
      "Requirement already satisfied: charset-normalizer~=2.0.0 in e:\\softwares\\anaconda\\lib\\site-packages (from requests>=2.9.1->conda-repo-cli==1.0.4) (2.0.4)\n",
      "Requirement already satisfied: urllib3<1.27,>=1.21.1 in e:\\softwares\\anaconda\\lib\\site-packages (from requests>=2.9.1->conda-repo-cli==1.0.4) (1.25.11)\n",
      "Requirement already satisfied: idna<4,>=2.5 in e:\\softwares\\anaconda\\lib\\site-packages (from requests>=2.9.1->conda-repo-cli==1.0.4) (3.3)\n",
      "Requirement already satisfied: pywin32>=1.0 in e:\\softwares\\anaconda\\lib\\site-packages (from jupyter-core->nbformat>=4.4.0->conda-repo-cli==1.0.4) (302)\n"
     ]
    }
   ],
   "source": [
    "!pip install conda-repo-cli==1.0.4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a557e35a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: anaconda-project==0.10.1 in e:\\softwares\\anaconda\\lib\\site-packages (0.10.1)\n",
      "Requirement already satisfied: conda-pack in e:\\softwares\\anaconda\\lib\\site-packages (from anaconda-project==0.10.1) (0.6.0)\n",
      "Requirement already satisfied: tornado>=4.2 in e:\\softwares\\anaconda\\lib\\site-packages (from anaconda-project==0.10.1) (6.1)\n",
      "Requirement already satisfied: ruamel-yaml in e:\\softwares\\anaconda\\lib\\site-packages (from anaconda-project==0.10.1) (0.17.21)\n",
      "Requirement already satisfied: anaconda-client in e:\\softwares\\anaconda\\lib\\site-packages (from anaconda-project==0.10.1) (1.9.0)\n",
      "Requirement already satisfied: requests in e:\\softwares\\anaconda\\lib\\site-packages (from anaconda-project==0.10.1) (2.26.0)\n",
      "Requirement already satisfied: jinja2 in e:\\softwares\\anaconda\\lib\\site-packages (from anaconda-project==0.10.1) (2.11.3)\n",
      "Requirement already satisfied: setuptools in e:\\softwares\\anaconda\\lib\\site-packages (from anaconda-client->anaconda-project==0.10.1) (61.2.0)\n",
      "Requirement already satisfied: pytz in e:\\softwares\\anaconda\\lib\\site-packages (from anaconda-client->anaconda-project==0.10.1) (2021.3)\n",
      "Requirement already satisfied: PyYAML>=3.12 in e:\\softwares\\anaconda\\lib\\site-packages (from anaconda-client->anaconda-project==0.10.1) (6.0)\n",
      "Requirement already satisfied: nbformat>=4.4.0 in e:\\softwares\\anaconda\\lib\\site-packages (from anaconda-client->anaconda-project==0.10.1) (5.3.0)\n",
      "Requirement already satisfied: clyent>=1.2.0 in e:\\softwares\\anaconda\\lib\\site-packages (from anaconda-client->anaconda-project==0.10.1) (1.2.2)\n",
      "Requirement already satisfied: six in e:\\softwares\\anaconda\\lib\\site-packages (from anaconda-client->anaconda-project==0.10.1) (1.16.0)\n",
      "Requirement already satisfied: python-dateutil>=2.6.1 in e:\\softwares\\anaconda\\lib\\site-packages (from anaconda-client->anaconda-project==0.10.1) (2.8.2)\n",
      "Requirement already satisfied: fastjsonschema in e:\\softwares\\anaconda\\lib\\site-packages (from nbformat>=4.4.0->anaconda-client->anaconda-project==0.10.1) (2.15.1)\n",
      "Requirement already satisfied: jsonschema>=2.6 in e:\\softwares\\anaconda\\lib\\site-packages (from nbformat>=4.4.0->anaconda-client->anaconda-project==0.10.1) (4.4.0)\n",
      "Requirement already satisfied: jupyter-core in e:\\softwares\\anaconda\\lib\\site-packages (from nbformat>=4.4.0->anaconda-client->anaconda-project==0.10.1) (4.10.0)\n",
      "Requirement already satisfied: traitlets>=4.1 in e:\\softwares\\anaconda\\lib\\site-packages (from nbformat>=4.4.0->anaconda-client->anaconda-project==0.10.1) (5.1.1)\n",
      "Requirement already satisfied: attrs>=17.4.0 in e:\\softwares\\anaconda\\lib\\site-packages (from jsonschema>=2.6->nbformat>=4.4.0->anaconda-client->anaconda-project==0.10.1) (21.4.0)\n",
      "Requirement already satisfied: pyrsistent!=0.17.0,!=0.17.1,!=0.17.2,>=0.14.0 in e:\\softwares\\anaconda\\lib\\site-packages (from jsonschema>=2.6->nbformat>=4.4.0->anaconda-client->anaconda-project==0.10.1) (0.18.0)\n",
      "Requirement already satisfied: idna<4,>=2.5 in e:\\softwares\\anaconda\\lib\\site-packages (from requests->anaconda-project==0.10.1) (3.3)\n",
      "Requirement already satisfied: charset-normalizer~=2.0.0 in e:\\softwares\\anaconda\\lib\\site-packages (from requests->anaconda-project==0.10.1) (2.0.4)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in e:\\softwares\\anaconda\\lib\\site-packages (from requests->anaconda-project==0.10.1) (2022.5.18.1)\n",
      "Requirement already satisfied: urllib3<1.27,>=1.21.1 in e:\\softwares\\anaconda\\lib\\site-packages (from requests->anaconda-project==0.10.1) (1.25.11)\n",
      "Requirement already satisfied: MarkupSafe>=0.23 in e:\\softwares\\anaconda\\lib\\site-packages (from jinja2->anaconda-project==0.10.1) (1.1.1)\n",
      "Requirement already satisfied: pywin32>=1.0 in e:\\softwares\\anaconda\\lib\\site-packages (from jupyter-core->nbformat>=4.4.0->anaconda-client->anaconda-project==0.10.1) (302)\n",
      "Requirement already satisfied: ruamel.yaml.clib>=0.2.6 in e:\\softwares\\anaconda\\lib\\site-packages (from ruamel-yaml->anaconda-project==0.10.1) (0.2.6)\n"
     ]
    }
   ],
   "source": [
    "!pip install anaconda-project==0.10.1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "535c79ff",
   "metadata": {},
   "source": [
    "接下来，导入相关的库。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6297daf5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "import numpy as np\n",
    "\n",
    "import tensorflow as tf\n",
    "assert tf.__version__.startswith('2')\n",
    "\n",
    "from tflite_model_maker import model_spec\n",
    "from tflite_model_maker import image_classifier\n",
    "from tflite_model_maker.config import ExportFormat\n",
    "from tflite_model_maker.config import QuantizationConfig\n",
    "from tflite_model_maker.image_classifier import DataLoader\n",
    "\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e0547024",
   "metadata": {},
   "source": [
    "# 模型训练\n",
    "\n",
    "## 获取数据\n",
    "\n",
    "本实验先从较小的[数据集](https://so.csdn.net/so/search?q=数据集&spm=1001.2101.3001.7020)开始训练，当然越多的数据，模型精度更高。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e00143e7",
   "metadata": {},
   "outputs": [],
   "source": [
    "image_path = tf.keras.utils.get_file(\n",
    "      'flower_photos.tgz',\n",
    "      'https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz',\n",
    "      extract=True)\n",
    "image_path = os.path.join(os.path.dirname(image_path), 'flower_photos')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2c0123cf",
   "metadata": {},
   "source": [
    "这里从storage.googleapis.com中下载了本实验所需要的数据集。image_path可以定制，默认是在用户目录的.keras\\datasets中。\n",
    "\n",
    "运行示例\n",
    "一共需4步完成。\n",
    "第一步：加载数据集，并将数据集分为训练数据和测试数据。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e9719bde",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:Load image with size: 3670, num_label: 5, labels: daisy, dandelion, roses, sunflowers, tulips.\n"
     ]
    }
   ],
   "source": [
    "data = DataLoader.from_folder(image_path)\n",
    "train_data, test_data = data.split(0.9)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3f9034b7",
   "metadata": {},
   "source": [
    "第二步：训练[Tensorflow](https://so.csdn.net/so/search?q=Tensorflow&spm=1001.2101.3001.7020)模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "28c289a5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:Retraining the models...\n",
      "Model: \"sequential\"\n",
      "_________________________________________________________________\n",
      " Layer (type)                Output Shape              Param #   \n",
      "=================================================================\n",
      " hub_keras_layer_v1v2 (HubKe  (None, 1280)             3413024   \n",
      " rasLayerV1V2)                                                   \n",
      "                                                                 \n",
      " dropout (Dropout)           (None, 1280)              0         \n",
      "                                                                 \n",
      " dense (Dense)               (None, 5)                 6405      \n",
      "                                                                 \n",
      "=================================================================\n",
      "Total params: 3,419,429\n",
      "Trainable params: 6,405\n",
      "Non-trainable params: 3,413,024\n",
      "_________________________________________________________________\n",
      "None\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "E:\\SoftWares\\Anaconda\\lib\\site-packages\\keras\\optimizers\\optimizer_v2\\gradient_descent.py:108: UserWarning: The `lr` argument is deprecated, use `learning_rate` instead.\n",
      "  super(SGD, self).__init__(name, **kwargs)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/5\n",
      "103/103 [==============================] - 191s 2s/step - loss: 0.8726 - accuracy: 0.7740\n",
      "Epoch 2/5\n",
      "103/103 [==============================] - 166s 2s/step - loss: 0.6586 - accuracy: 0.8914\n",
      "Epoch 3/5\n",
      "103/103 [==============================] - 149s 1s/step - loss: 0.6211 - accuracy: 0.9193\n",
      "Epoch 4/5\n",
      "103/103 [==============================] - 148s 1s/step - loss: 0.6044 - accuracy: 0.9245\n",
      "Epoch 5/5\n",
      "103/103 [==============================] - 141s 1s/step - loss: 0.5934 - accuracy: 0.9305\n"
     ]
    }
   ],
   "source": [
    "inception_v3_spec = image_classifier.ModelSpec(uri='https://storage.googleapis.com/tfhub-modules/tensorflow/efficientnet/lite0/feature-vector/2.tar.gz')\n",
    "inception_v3_spec.input_image_shape = [240, 240]\n",
    "model = image_classifier.create(train_data, model_spec=inception_v3_spec)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7886e9a9",
   "metadata": {},
   "source": [
    "第三步：评估模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e48f8a9d",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12/12 [==============================] - 22s 1s/step - loss: 0.5907 - accuracy: 0.9264\n"
     ]
    }
   ],
   "source": [
    "loss, accuracy = model.evaluate(test_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "ffe2488b",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:Assets written to: C:\\Users\\luke\\AppData\\Local\\Temp\\tmpk1r07g6y\\assets\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:Assets written to: C:\\Users\\luke\\AppData\\Local\\Temp\\tmpk1r07g6y\\assets\n",
      "E:\\SoftWares\\Anaconda\\lib\\site-packages\\tensorflow\\lite\\python\\convert.py:766: UserWarning: Statistics for quantized inputs were expected, but not specified; continuing anyway.\n",
      "  warnings.warn(\"Statistics for quantized inputs were expected, but not \"\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:Label file is inside the TFLite model with metadata.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:Label file is inside the TFLite model with metadata.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:Saving labels in C:\\Users\\luke\\AppData\\Local\\Temp\\tmpuor3qt0h\\labels.txt\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:Saving labels in C:\\Users\\luke\\AppData\\Local\\Temp\\tmpuor3qt0h\\labels.txt\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:TensorFlow Lite model exported successfully: .\\model.tflite\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:TensorFlow Lite model exported successfully: .\\model.tflite\n"
     ]
    }
   ],
   "source": [
    "model.export(export_dir='.')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c5cff9f",
   "metadata": {},
   "source": [
    "这里导出的Tensorflow Lite模型包含了元数据(metadata),其能够提供标准的模型描述。导出的模型存放在Jupyter Notebook当前的工作目录中。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "deb9fbe5",
   "metadata": {},
   "source": [
    "![image-20220527165332956](https://gitee.com/deer-guide/my-drawing-bed/raw/master/202205271653534.png)\n",
    "\n",
    "删除了原本的Ml,替换为现在新生成的，接下来查看效果如图：\n",
    "![image-20220527165700599](https://gitee.com/deer-guide/my-drawing-bed/raw/master/202205271657877.png)\n",
    "![image-20220527171047817](https://gitee.com/deer-guide/my-drawing-bed/raw/master/202205271710354.png)\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
